package provider;

import com.liCheng.entity.Provider;
import com.liCheng.service.provider.ProviderService;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

public class ProviderTest {
    private static Logger logger=Logger.getLogger(ProviderTest.class);

    /**
     * 显示所有的供应商数据
     */
    @Test
    public void testGetProviderList() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext-mybatis.xml");
        ProviderService providerService = (ProviderService) ctx
                .getBean("providerService");
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.findProviderWithConditions();

        for (Provider provider : providerList) {
            logger.debug("testGetProviderList proCode: "
                    + provider.getProCode() + " and proName: "
                    + provider.getProName());
        }
    }

    /**
     * 根据供应商名称模糊查询
     */
    @Test
    public void testGetProviderList1() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext-mybatis.xml");
        ProviderService providerService = (ProviderService) ctx
                .getBean("providerService");
        List<Provider> providerList = new ArrayList<Provider>();
        String proName="北";
        providerList = providerService.providerName1(proName);

        for (Provider provider : providerList) {
            logger.debug("testGetProviderList proCode: "
                    + provider.getProCode() + " and proName: "
                    + provider.getProName());
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Test
    public void testGetProviderUpdate() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext-mybatis.xml");
        ProviderService providerService = (ProviderService) ctx
                .getBean("providerService");
        int count=0;
        Provider provider=new Provider();
        provider.setId(18);
        provider.setProCode("wdw");
        provider.setProName("fwf");
        count = providerService.proUpdateTest(provider);
        if (count>0){
            logger.debug("修改成功");
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Test
    public void testGetProviderDel() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext-mybatis.xml");
        ProviderService providerService = (ProviderService) ctx
                .getBean("providerService");
        int count=0;
        int id=19;
        count = providerService.proDelTest(id);
        if (count>0){
            logger.debug("删除成功");
        }
    }

}
